//
//  DemViewController.m
//  iHealthEHR
//
//  Created by Joseph Ayo Akinyele on 3/27/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "DemViewController.h"
#import "DemographicsCell.h"

@implementation DemViewController
@synthesize personalInformation;
@synthesize keyOrder;

- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}

- (void)viewDidLoad {
	// set up keys
	if(keyOrder == nil) {
		keyOrder = [[NSArray alloc] initWithObjects:@"DOB", @"Gender", @"Race", @"Blood Type", @"Height", @"Weight", nil];
	}
	
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewWillAppear:(BOOL)animated {
	[self.tableView reloadData];
    [super viewWillAppear:animated];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [personalInformation count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *DemographicsCellIdentifier = @"DemographicsCellIdentifier";
    
    DemographicsCell *cell = (DemographicsCell *)[tableView dequeueReusableCellWithIdentifier:DemographicsCellIdentifier];
    if (cell == nil) {
 		// load custom nib life
		NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DemographicsCell" 
													 owner:self 
												   options:nil];
		// retrieve cell object in nib file.
		cell = [nib objectAtIndex:0];
    }	
    
    // Set up the cell...
	NSUInteger row = [indexPath row];
	NSString *key = [keyOrder objectAtIndex:row];	
	NSString *value = [personalInformation objectForKey:key];
		
	cell.label.text = key;
	if([key isEqualToString:@"Height"]) {
		// convert into feet and inches
		cell.content.text = [self convertHeight:value];
	}
	else if([key isEqualToString:@"Weight"]) {
		// convert into Lbs and oz.
		cell.content.text = [self convertWeight:value];
	}
	else {
		cell.content.text = value;
	}
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
	return;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
	[keyOrder release];
	[personalInformation release];
    [super dealloc];
}

#pragma mark -
#pragma mark Internal functions for displaying height and Weight
-(NSString *) convertHeight:(NSString *) heightValue
{
	int h = [heightValue intValue];
	
	int feet = h / 12;
	int inches = h % 12;
	
	if(inches == 0) {
		return [NSString stringWithFormat:@"%d ft.", feet];
	}
	
	return [NSString stringWithFormat:@"%d' %d in.", feet, inches];
}

-(NSString *) convertWeight:(NSString *) weightValue
{
	int w = [weightValue intValue];
	
	int pounds = w / 16;
	int ounces = w % 16;
	
	if(ounces == 0) {
		return [NSString stringWithFormat:@"%d lbs.", pounds];
	}
	
	return [NSString stringWithFormat:@"%d lbs, %d oz.", pounds, ounces];	
}

@end

